<?php


namespace app\common\controller;

use think\Controller;
use app\common\model\Article as ArticleModel;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\Exception\DbException;
use think\exception\PDOException;

class Article extends Controller
{

    /*-===================================================== 查询==================================================*/
//    查询所有文章
    public function selectAllArticle(){
        $article = new ArticleModel();
        try {
            return  $data = $article
                ->where("article_delete_time",null)
                ->order("article_create_time desc")
                ->all();
        } catch (DataNotFoundException $e) {
        }
    }


    //    多表 分页 查询 classes-article
    public function selectArticleAndClassPage($count){
        try {
            return Db::table('b_article')
                ->alias('a')
                ->join('b_classes c', 'a.article_class = c.class_id')
                ->where("article_delete_time",null)
                ->order("article_create_time desc")
                ->paginate($count);
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }
    }

    // 分页查询 所有文章
    public function selectAllArticlePage($count){
        $article = new ArticleModel();
        try {
            return  $data = $article
                    ->where("article_delete_time",null)
                    ->order("article_create_time desc")
                    ->paginate($count);
        } catch (DataNotFoundException $e) {
        }
    }

//    根据分类查询 部分文章
    public function selectLimitArticle($article_class, $count){
        $article = new ArticleModel();
        try {
            return  $article
                ->where("article_class",$article_class)
                ->where("article_delete_time",null)
                ->order("article_create_time desc")
                ->limit($count)
                ->select();
        } catch (DbException $e) {
        }
    }

//    按照分类查询所有未删除的数据
//    public function selectArticle($article_class){
//        $article = new ArticleModel();
//        try {
//            return  $article
//                ->where("article_class",$article_class)
//                ->where("article_delete_time",null)
//                ->order("article_create_time desc")
//                ->select();
//        } catch (DbException $e) {
//        }
//    }
    //    按照分类查询所有未删除的数据
    public function selectArticleLimit($article_class,$limitCount){
        $article = new ArticleModel();
        try {
            return  $article
                ->where("article_class",$article_class)
                ->where("article_delete_time",null)
                ->order("article_create_time desc")
                ->limit($limitCount)
                ->select();
        } catch (DbException $e) {
        }
    }

    //    分类 查询 连表 分页
    public function selectAllArtAndClaPage($article_class,$count){
        try {
            return Db::table('b_article')
                ->alias('a')
                ->join('b_classes c', 'a.article_class = c.class_id')
                ->where("article_class",$article_class)
                ->where("article_delete_time",null)
                ->order("article_create_time desc")
                ->paginate($count);
        } catch (DbException $e) {
        }
    }

//   根据文章id查询数据一条数据
    public  function selectArticleFromId($articleId){
        $article = new ArticleModel();
        try {
            return  $article
                ->where("article_id",$articleId)
                ->where("article_delete_time",null)
                ->find();
        } catch (DbException $e) {
        }
    }

//    多表查询 classes-article
//    public function selectArticleAndClass(){
//        try {
//            return Db::table('b_article')
//                ->alias('a')
//                ->join('b_classes c', 'a.article_class = c.class_id')
//                ->select();
//        } catch (DataNotFoundException $e) {
//        } catch (ModelNotFoundException $e) {
//        } catch (DbException $e) {
//        }
//    }



    //    多表查询 classes-article
    public function selectArtAndClaFromClaId($articleId){
        try {
            return Db::table('b_article')
                ->alias('a')
                ->join('b_classes c', 'a.article_class = c.class_id')
                ->where("article_id",$articleId)
                ->where("article_delete_time",null)
                ->find();
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }
    }

// 查询该分类下 下一条

// 查询该分类下 上一条
    /*-===================================================== 插入 ==================================================*/


    public function insertArticle($articleName,$articleContent,$articleContentText,$articleClass){
        $data = [
            'article_name'          => $articleName,
            'article_content'       => $articleContent,
            'article_content_text'  => $articleContentText,
            'article_class'         => $articleClass,
            'article_create_time'   => date('Y-m-d H:i:s')
        ];
        return Db::table('b_article')->insert($data);
    }


    /*====================================================== 删除 =============================================*/


//    根据文章编号删除
    public function SoftDelArticleById($articleId){
        try {
            return Db::table('b_article')
                ->where('article_id', $articleId)
                ->useSoftDelete('article_delete_time', date('Y-m-d H:i:s'))
                ->delete();
        } catch (PDOException $e) {
        } catch (Exception $e) {
        }
    }


    /*===================================更新===============================================*/

    public function updateArticle($articleId,$articleName,$articleContent,$articleContentText,$articleClass){
        $data = [
            'article_name'          => $articleName,
            'article_content'       => $articleContent,
            'article_content_text'  => $articleContentText,
            'article_class'         => $articleClass,
            'article_update_time'   => date('Y-m-d H:i:s')
        ];
        try {
            return Db::table('b_article')
                ->where('article_id', $articleId)
                ->update($data);
        } catch (PDOException $e) {
        } catch (Exception $e) {
        }
    }


    /*   上一条  */
    public function selectLastForTimeById($articleId){
        $time = Db::table('b_article')->where('article_id', $articleId)->find()['article_create_time'];
        try {
            return Db::table('b_article')
                ->where("article_create_time", ">", $time)
                ->order('article_create_time', "asc")
                ->find();
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }
    }
    /*  下一条  */
    public function selectNextForTimeById($articleId){
        $time = Db::table('b_article')->where('article_id', $articleId)->find()['article_create_time'];
        try {
            return Db::table('b_article')
                ->where("article_create_time", "<", $time)
                ->order('article_create_time', "desc")
                ->find();
        } catch (DataNotFoundException $e) {
        } catch (ModelNotFoundException $e) {
        } catch (DbException $e) {
        }
    }
}